public class UpdateReferalPayments{

    public static void PaymentDetails(Referral_Payment__c[] RP){     
     
       for (Referral_Payment__c R:RP){

        /*Update Referal Object when Payment is received */
        
        IF(R.Referral_ID__c != null){
         Referrals__c Ref= [Select No_of_Gifts__c,Total_Payment_Made__c from Referrals__c where ID =:R.Referral_ID__c];
         if(Ref.ID != null){
         Referral_Payment__c[] rrp = [Select Gift_Amount_Paid__c,Gifts_Redeemed__c from Referral_Payment__c where Referral_ID__c =:Ref.ID];
         Double Amount=0;
         Double Gifts=0;
         
         IF(rrp.size()>0){
         for(integer i=0;i<rrp.size();i++){
         Amount+= rrp[i].Gift_Amount_Paid__c;
         Gifts += rrp[i].Gifts_Redeemed__c;
         }
         Ref.Total_Payment_Made__c = Amount;
         //Ref.No_of_Gifts__c = Ref.No_of_Gifts__c - Gifts;
         Ref.Gifts_to_be_redeemed__c = Ref.No_of_Gifts__c - Gifts;
         Update Ref;
         }
         }        
        }
       } 
     }    
  static testMethod void UpdateReferalPayments()
  {
   Account[] acc =new Account[]
  {
  new Account(LastName='XYZ',Email_acc__c='xyz@gmail.com',Payment_Made__c = False)
  };
  Insert acc;
  Channel_Partner__c[] CP=new Channel_Partner__c[]
{
new Channel_Partner__c(Name='Aparnafirms',Type_of_Channel_Partner__c='Domestic',Email__c='xy@hotmail.com')
};
Insert CP;
Payout_percentage__c[] pp=new Payout_percentage__c[]
{
new Payout_percentage__c(Channel_Partner__c=CP[0].id,Payout_Percentage__c=2)
};
Insert pp;
Referrals__c[] ref=new Referrals__c[]
{
new Referrals__c(Referral_Name__c=acc[0].id)
};
Insert ref;
Opportunity[] opp=new Opportunity[]
{
new Opportunity(Name='XYZ', AccountID=acc[0].id,StageName='Closed won',Application_Form__c = true,CRM_form__c = true, LOI_Sent__c= true,Agreement_Signed__c = true,CloseDate=Date.newInstance(2008,02,02),ForecastCategoryName='Best Case',Project_Type__c='Aspirational',LeadSource='Existing Customer',ReferralName__c=acc[0].id)
};
Insert opp;
Lead[] L1=new Lead[]
{
new Lead(Project_Interested__c='Marina',Mobile__c='2345678912',LastName='Technologies',Interested__c='yes',Status='Visit Confirmed',LeadSource = 'Web',Date_Of_Visit__c=Datetime.newInstance(2008, 10, 28, 1, 6, 8))
};
Insert L1;

Opportunity[] opp1=new Opportunity[]
{
new Opportunity(Name='XYZ', AccountID=acc[0].id,StageName='Closed won',Application_Form__c = true,CRM_form__c = true, LOI_Sent__c= true,Agreement_Signed__c = true,CloseDate=Date.newInstance(2008,02,02),ForecastCategoryName='Best Case',Project_Type__c='Aspirational',LeadSource='Channel Partner',Channel_Partner__c=CP[0].id,Amount=1000000 ,Lead_ID__c=L1[0].id)
};
Insert opp1;

Opportunity[] opp2=new Opportunity[]
{
new Opportunity(Name='XYZ', AccountID=acc[0].id,StageName='Cancelled',CloseDate=Date.newInstance(2008,02,02),ForecastCategoryName='Best Case',Project_Type__c='Aspirational',LeadSource='Channel Partner',Channel_Partner__c=CP[0].id,Amount=1000000)
};
Insert opp2;

Lead led1 = new Lead();
led1.status='Follow up';
led1.LeadSource = 'Channel Partner';
led1.Project_Interested__c ='Marine';
led1.Mobile__c='1234567893';
led1.LastName='XXXXXX';
led1.Interested__c='Yes';
led1.Follow_Up_Date__c=Datetime.newInstance(2008, 10, 28, 1, 6, 8);
led1.Channel_Partner__c=CP[0].id;

//insert led1;

Campaign[] cam =new Campaign[]
{
new Campaign(Name='ABC',Ad_Communication__c='Product', Total_Call_Attempts__c = 0)};
Insert cam; 

/*
Call_Attempts__c[] MS2 =new Call_Attempts__c[]
{
new Call_Attempts__c(lead__c =Opp2[0].Lead_id__c,CampaignUsed__c = cam[0].id,Call_Status__c='Lodha a1qua')
};
Insert MS2;

Call_Attempts__c[] MS1 =new Call_Attempts__c[]
    {
    new Call_Attempts__c(lead__c =opp1[0].Lead_ID__c,CampaignUsed__c = cam[0].id,Call_Status__c='Lodha a1qua')
    };
    Insert MS1; 
    */
Payout_percentage__c[] pp1=new Payout_percentage__c[]
{
new Payout_percentage__c(Lead__c=led1.id,Channel_Partner__c=led1.Channel_partner__c,Payout_Percentage__c=2)
};
Insert pp1;
/*Task[] ts = new Task[2];
ts[0] = new Task(WHOID=L1[0].Id,OWNERID=UserInfo.getUserId(),Subject='Test',priority ='High',status = 'Not started',Mail_Check_Flag__c = 'YES');
insert ts[0];*/


  Lead[] Ld=new Lead[]
  {
  new Lead(OWNERID=UserInfo.getUserId(),Company='Wipro',Project_Interested__c='Marine',Mobile__c='2345678412',LastName='Technologies',LeadSource='Web',Interested__c='yes',Status='Open')
  };
  insert Ld;

  Referrals__c[] ref1=new Referrals__c[]
  {
  new Referrals__c(Referral_Name__c = acc[0].id,Referred_Customer__c=Ld[0].id)
  };
  Insert ref1;
  Date todaydate = date.today();
  
   Referral_Payment__c[] RP=new  Referral_Payment__c[]
   {
   new  Referral_Payment__c(Referral_ID__c=ref[0].id,Date_of_Payment__c = todaydate ,Gift_Amount_Paid__c=10000,Gifts_Redeemed__c=0)
   };
   Insert RP;
   Lead[] L2=new Lead[]
    {
    new Lead(Company='Wipro',Project_Interested__c='LodhaMarine',Mobile__c='2345667892',LastName='Technologies',LeadSource='Web')
    };
   //  Insert L2;
     UpdateReferalPayments.PaymentDetails(RP);
     //LODHA_UpdateProjectInterested.Updatemethod(L2);
   }
   }